package com.tengju.data.infrastructure.repository;

import com.tengju.data.domain.model.motivation.DwsBonusTopMonthRepository;
import com.tengju.data.domain.shared.Month;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.data.custom.DwsBonusTopMonthMapper;
import com.tengju.user.domain.model.Row;
import com.tengju.user.domain.model.user.UserIdCode;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Repository
public class DwsBonusTopMonthRepositoryImpl implements DwsBonusTopMonthRepository {

    @Resource
    DwsBonusTopMonthMapper dwsBonusTopMonthMapper;


    /**
     * 月度收益个人排行
     * @return
     */
    @Override
    public Row getSelfMonthRank(UserIdCode userIdCode, Integer month,Integer lastMonth, Integer topLimit) {

        Map<String,Object> map = dwsBonusTopMonthMapper.getSelfMonthRank(userIdCode.getValue(),
                month,
                lastMonth,
                topLimit);
        return new Row(map);
    }

    /**
     * 月度收益总排行
     */
    @Override
    public List<Row> getAllMonthRank(Integer month, Integer topLimit,Integer offset,Integer limit) {

        List<Map<String,Object>> mapList =
                dwsBonusTopMonthMapper.getAllMonthRank(month,topLimit,offset,limit);
        if (CollectionUtils.isEmpty(mapList)) {
            return Collections.emptyList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> getByIdCodeAndMonth(List<UserIdCode> idCodeList, Integer month, Integer topLimit) {
        if (CollectionUtils.isEmpty(idCodeList)) {
            return Collections.emptyList();
        }
        List<Long> userIdCodeList = idCodeList.stream()
                .filter(Objects::nonNull)
                .map(UserIdCode::getValue)
                .collect(Collectors.toList());
        List<Map<String,Object>> mapList =
                dwsBonusTopMonthMapper.getByIdCodeAndMonth(userIdCodeList,month,topLimit);
        if (CollectionUtils.isEmpty(mapList)) {
            return Collections.emptyList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public Integer getTotal(Integer month, Integer topLimit) {
        return dwsBonusTopMonthMapper.getTotal(month,topLimit);
    }


}
